package d5.offer50;

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public char firstUniqChar(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char key = s.charAt(i);
            //重复增加
            if (map.containsKey(key)) {
                map.put(key, -1);
                continue;
            }
            //第一次增加
            map.put(key, i);
        }
        // 不取s.length - 1，因为s可能为空字符串
        int first = s.length();
        for (Character key : map.keySet()) {
            Integer index = map.get(key);
            if (index != -1 && index < first) {
                first = index;
            }
        }
        return first == s.length() ? ' ' : s.charAt(first);
    }

    public static void main(String[] args) {
//        输入：s = "aba"
//        输出：'b'
        String s = "aaccdeff";
        char c = new Solution().firstUniqChar(s);
        System.out.println(":" + c + ":");
    }
}
